package com.lexy.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class CommMethod {
	
	
	public static Document getDocument(String urlStr) {
		Document document = null;
		try {
			document = Jsoup.connect(urlStr).userAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36").get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return document;
	}
	
//	http://www.aimeishishang.com/ArisaraKarbdecho/
	
	
	
	
	public static List<String> getDivHrefList(Document document) {
		
		List<String> list = new ArrayList<String>();
		
		Element elementById = document.getElementById("waterfall");
		
		
		Elements elementsByAttributeValue = elementById.getElementsByAttributeValue("class","wxm-imglist-box");
		
		System.out.println(elementsByAttributeValue.size());
		
		return null;
		
		
	}
	
	
	public static List<String> getDocumentPageNum(String urlStr ) {
		Document document = getDocument(urlStr);
		
		List<String> list = new ArrayList<String>();
		
		Elements allElements = document.getElementById("fd_page_bottom").getAllElements();
		
		Elements allElements2 = allElements.get(0).getAllElements();
		for (int i =0 ; i< allElements2.size() ; i++) {
			if(i==allElements2.size()-1){
				break;
			}
			Element element = allElements2.get(i);
			String attr = element.attr("href");
			if(attr!=null && attr!=""){
				list.add(attr);
				System.out.println(attr);
			}
		}
		
		
		
		return list;
	}
	
	
	
	
	
	
	public static void createFileDir(String fileDir){
		File fileDirTitle = new File(fileDir);
		if(!fileDirTitle.exists()){
			fileDirTitle.mkdirs();
		}
	}
	
	
	
	
	public static void  downLoadFile(String Url , String fileDir){
		try {
			byte[] imgByte = HttpPostUtils.getImgByte(Url);
			FileOutputStream fos = new FileOutputStream(new File(fileDir));
			fos.write(imgByte);
		} catch (Exception e) {
			System.out.println("Url:"+Url +", 下载失败！");
		}
	}
	
	
	
	
	
	
	
	
	public static void main(String[] args) {
		String urlStr = "http://www.aimeishishang.com/ArisaraKarbdecho/";
		
		Document document = getDocument(urlStr);
		
		List<String> divHrefList = getDivHrefList(document);
	}

}
